#import "JetourLinePlugin.h"
#import "AhaAmapFlutterStreamManager.h"
#import "FlutterActivityIndicator.h"
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import "ToastUtil.h"
#import "BaseLocationUtils.h"
#import "AhaNaviUtils.h"
static NSObject<FlutterPluginRegistrar>* flutterRegister;

@interface JetourLinePlugin()
@property (nonatomic,strong)BaseLocationUtils *baseLocationUtils;

@end


@implementation JetourLinePlugin
- (BaseLocationUtils *)baseLocationUtils{
    if(_baseLocationUtils == NULL){
        _baseLocationUtils = [[BaseLocationUtils alloc]init];
    }return _baseLocationUtils;
}
+ (void)setCallback:(MapMethodCallBack)callback{
    methodCallBack = callback;
}
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    flutterRegister = registrar;
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"amap_flutter_plugin"
            binaryMessenger:[registrar messenger]];
  JetourLinePlugin* instance = [[JetourLinePlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];

  FlutterEventChannel *eventChanel = [FlutterEventChannel eventChannelWithName:@"amap_flutter_plugin_stream" binaryMessenger:[registrar messenger]];
  [eventChanel setStreamHandler:[[AhaAmapFlutterStreamManager sharedInstance] streamHandler]];
    
    
  [registrar registerViewFactory:[[FlutterActivityIndicatorFactory alloc] initWithMessenger:registrar.messenger] withId:@"aha_map_view"];
}

+ (NSObject<FlutterPluginRegistrar>*)getRegister{
    return flutterRegister;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    if ([@"buyLine" isEqualToString:call.method]) {//购买路书
        if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) {
            
            NSString *urlString = [[NSString stringWithFormat:call.arguments] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
            //iOS10以后,使用新API
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
                NSLog(@"scheme调用结束");
            }];
        } else {
            //iOS10以前,使用旧API
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:call.arguments] ];
        }
        
    }else
    if ([@"showTxt" isEqualToString:call.method]) {//toast
        NSString *txt = (NSString *)call.arguments;
        [[[ToastUtil alloc]init] showToast:txt];
    }if ([@"getMaps" isEqualToString:call.method]) {//获取安装的地图
        NSMutableArray *arr = [NSMutableArray array];
        if( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
            [arr addObject:@"百度地图"];
        }if( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
            [arr addObject:@"高德地图"];
        }if( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
            [arr addObject:@"腾讯地图"];
        }
        [arr addObject:@"苹果地图"];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:arr forKey:@"maps"];
        [dic setValue:@"getMaps" forKey:@"method"];
        [self handlePlugin:dic];
    }else if ([@"buyLine" isEqualToString:call.method]) {//购买路书
        NSURL *URL = [NSURL URLWithString:call.arguments];
        [[UIApplication sharedApplication]openURL:URL];

    }else if ([@"naviGo" isEqualToString:call.method]) {//导航
        [AhaNaviUtils goNavi:call.arguments];

    }else if ([@"startLocation" isEqualToString:call.method]) {
        __weak typeof(self) wself = self;
        [self.baseLocationUtils startLocation:^(NSDictionary *dic) {
            [wself handlePlugin:dic];
        }];
    }else if ([@"set_map_key" isEqualToString:call.method]) {
     NSDictionary *dic = call.arguments;
     [[AMapServices sharedServices] setEnableHTTPS:YES];
     [AMapServices sharedServices].apiKey = [dic objectForKey:@"ios"];
  } else {//地图方法
      if(methodCallBack != nil){
          methodCallBack(call.arguments);
      }
  }
}


- (void)handlePlugin:(NSDictionary *)dic
{
    if (![[AhaAmapFlutterStreamManager sharedInstance] streamHandler].eventSink) {
        return;
    }
    [[AhaAmapFlutterStreamManager sharedInstance] streamHandler].eventSink(dic);
}



 
@end
